﻿
using System;
using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Dao;
using MiniWeb.Core.Entities;
using MiniWeb.Task.Config;
using WFramework.MvcPager;

namespace MiniWeb.Task
{
    public class OrdersTask
    {
        private readonly OrdersDao _dao = new OrdersDao();
        private readonly OrdersDetailDao _ordersDetailDao = new OrdersDetailDao();
        private readonly ShopCartTask _shopCartTask = new ShopCartTask();
        private readonly AccountsInfoDao _accountsInfoDao = new AccountsInfoDao();
        private readonly ProductsInfoDao _productsInfoDao = new ProductsInfoDao();

        public PagedList<Orders> GetPagedList(string account, string code, int? paytype, int? orderstate, DateTime? starttime, DateTime? endtime, int pageIndex, int pageSize)
        {
            return _dao.GetPagedList(account, code, paytype, orderstate, starttime, endtime, pageIndex, pageSize);
        }


        public Orders GetById(int code)
        {
            return _dao.GetById(code);
        }

        public void Delete(int code)
        {

            _dao.Delete(code);
        }

        public string CreateCode()
        {
            return string.Format("CS{0}", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
        }

        //public bool CreateOrder(string account, out string msg,out string ordercode)
        //{
        //    msg = string.Empty;
        //    ordercode = string.Empty;
        //    var shopcartlist = _shopCartTask.GetCartList(account);
        //    if (shopcartlist.Count < 1)
        //    {
        //        msg = "请选择购买商品";
        //        return false;
        //    }
        //    string code = CreateCode();
        //    ordercode = code;
        //    decimal OrderRMB = shopcartlist.Sum(c => c.TotalRMB);
        //   _dao.Add(new Orders
        //                                 {
        //                                     Code = code,
        //                                     Account = account,
        //                                     CreateDate = DateTime.Now,
        //                                     Num = shopcartlist.Count,
        //                                     PayPoin = 0,
        //                                     PayRMB = 0,
        //                                     FinishDate = null,
        //                                     PayType = (int)PayTypeEnum.未支付,
        //                                     RMB = OrderRMB,
        //                                     State = (int)OrderStateEnum.待支付,
        //                                     YouFeiRMB = WebConfig.YouFeiRMB,
        //                                     TotalRMB = OrderRMB + WebConfig.YouFeiRMB,
        //                                 });
        //    Orders orderinfo = _dao.GetByCode(code);
        //    foreach (var item in shopcartlist)
        //    {
        //        _ordersDetailDao.Add(new OrdersDetail
        //                                 {
        //                                     PeiZhi = item.PeiZhi,
        //                                     Color = item.Color,
        //                                     CreateDate = DateTime.Now,
        //                                     OrderCode = orderinfo.Code,
        //                                     Price = item.Price,
        //                                     ProductID = item.ProductID,
        //                                     ProductName = item.ProductName,
        //                                     ProductImg = item.SmallProductImg,
        //                                     Num = item.Num,
        //                                     TotalRMB = item.TotalRMB
        //                                 });
        //    }
        //    _shopCartTask.DeleteCartByAccount(account);
        //    return true;

        //}

        //public bool FastBuy(string account, int pid, int color, int peizhi, int num, out string msg,out string order)
        //{
        //    msg = string.Empty;
        //    order = string.Empty;
        //    var productinfo = _productsInfoDao.GetById(pid);
        //    if (productinfo == null)
        //    {
        //        msg = "产品不存在";
        //        return false;
        //    }
        //    string code = CreateCode();
        //    order = code;
        //   _dao.Add(new Orders
        //    {
        //        Code = code,
        //        Account = account,
        //        CreateDate = DateTime.Now,
        //        Num = num,
        //        PayPoin = 0,
        //        PayRMB = 0,
        //        FinishDate = null,
        //        PayType = (int)PayTypeEnum.未支付,
        //        RMB = productinfo.Price * num,
        //        YouFeiRMB = WebConfig.YouFeiRMB,
        //        TotalRMB = productinfo.Price * num + WebConfig.YouFeiRMB,
        //        State = (int)OrderStateEnum.待支付,

        //    });
        //    Orders orderinfo = _dao.GetByCode(code);
        //    _ordersDetailDao.Add(new OrdersDetail
        //    {
        //        PeiZhi = peizhi,
        //        Color = color,
        //        CreateDate = DateTime.Now,
        //        OrderCode = orderinfo.Code,
        //        Price = productinfo.Price,
        //        ProductID = productinfo.ID,
        //        ProductName = productinfo.ProductName,
        //        ProductImg = productinfo.ProductImg,
        //       Num = num,
        //        TotalRMB = productinfo.Price * num
        //    });

        //    return true;
        //}

        public Orders GetByCode(string code)
        {
            return _dao.GetByCode(code);
        }
        /// <summary>
        /// paytype 支付方式 1为人民币，2为西瓦劵
        /// </summary>
        /// <param name="code"></param>
        /// <param name="account"></param>
        /// <param name="webpaytype"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        //public bool Pay(string code, string account, int paytype, out string msg)
        //{
        //    msg = string.Empty;
        //    var orderinfo = _dao.GetByCode(code);
        //    if (orderinfo == null)
        //    {
        //        msg = "无该订单号";
        //        return false;
        //    }
        //    if (orderinfo.State != (int)OrderStateEnum.待支付)
        //    {
        //        msg = "该订单已取消或者已支付";
        //        return false;

        //    }
        //    var accountinfo = _accountsInfoDao.GetByAccount(account);
        //    if (accountinfo == null)
        //    {
        //        msg = "账号出错";
        //        return false;
        //    }
        //    if(accountinfo.Account !=orderinfo.Account)
        //    {
        //        msg = "账号异常";
        //        return false;
        //    }

        //    if (paytype == 1)
        //    {
        //        if (accountinfo.RMB < orderinfo.TotalRMB)
        //        {
        //            msg = "账号人民币不足，请充值";
        //            return false;
                   
        //        }
        //        accountinfo.RMB = accountinfo.RMB - orderinfo.TotalRMB;
        //        orderinfo.PayType = (int)PayTypeEnum.人民币;

        //    }
        //    if (paytype == 2)
        //    {
        //        if (accountinfo.Point < orderinfo.TotalRMB * WebConfig.Rate)
        //        {
        //            msg = "账号西瓦劵不足，请充值";
        //            return false;
                    
        //        }
        //        accountinfo.Point = accountinfo.Point - (int)(orderinfo.TotalRMB * WebConfig.Rate);
        //        orderinfo.PayType = (int)PayTypeEnum.西瓦劵;
        //    }
        //    _accountsInfoDao.Update(accountinfo);
           
        //    orderinfo.State = (int)OrderStateEnum.已支付;
        //    _dao.Update(orderinfo);

        //    return true;
        //}

        public bool Exists(int code)
        {

            return _dao.Exists(code);
        }

        public void Add(Orders model)
        {

            _dao.Add(model);
        }

        public void Update(Orders model)
        {

            _dao.Update(model);
        }

        public List<Orders> GetAll(int? top, string strwhere, string orderby)
        {
            return _dao.GetAll(top, strwhere, orderby);
        }



    }
}